/**
 * 
 */
package three_star_craft.gui;

import java.util.List;

import javax.swing.table.AbstractTableModel;

import ProtocolsCommon.Common.PlayableItem;
import ProtocolsCommon.Common.PlayableItem.ItemType;

/**
 * @author w.antosiewic
 *
 */
public class MediaTableModel extends AbstractTableModel
{
    private String[] columnNames = { "Type", "Name", "Author", "Size [kb]"};
    
    private List<PlayableItem> _media;
    
    void updateMedia(List<PlayableItem> media)
    {
        _media = media;
        fireTableDataChanged();
    }
    
    /* (non-Javadoc)
     * @see javax.swing.table.TableModel#getColumnCount()
     */
    public int getColumnCount()
    {
        return columnNames.length;
    }

    /* (non-Javadoc)
     * @see javax.swing.table.TableModel#getRowCount()
     */
    public int getRowCount()
    {
        return _media != null ? _media.size() : 0;
    }

    /* (non-Javadoc)
     * @see javax.swing.table.TableModel#getValueAt(int, int)
     */
    public Object getValueAt(int row, int col)
    {
        if(row < 0 || row >= _media.size())
        {
            return null;
        }
        PlayableItem item = _media.get(row);
        switch(col)
        {
        case 0: return item.getType() == ItemType.TYPE_AUDIO ? "A" : "V";
        case 1: return item.getName();
        case 2: return item.hasAuthor() ? item.getAuthor() : "";
        case 3: return item.getSize();
        }
        return null;
    }

    /* (non-Javadoc)
     * @see javax.swing.table.AbstractTableModel#getColumnClass(int)
     */
    @Override
    public Class<?> getColumnClass(int columnIndex)
    {
        switch(columnIndex)
        {
        case 0: return String.class;
        case 1: return String.class;
        case 2: return String.class;
        case 3: return Integer.class;
        }
        return String.class;
    }

    /* (non-Javadoc)
     * @see javax.swing.table.AbstractTableModel#getColumnName(int)
     */
    @Override
    public String getColumnName(int column)
    {
        return columnNames[column]; 
    }

    /* (non-Javadoc)
     * @see javax.swing.table.AbstractTableModel#isCellEditable(int, int)
     */
    @Override
    public boolean isCellEditable(int rowIndex, int columnIndex)
    {
        return false;
    }

    public PlayableItem getItem(int row)
    {
        return _media.get(row);
    }
}
